home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual BASIC 5.0 (Ent. Edition) / Vb5ent Extractor.EXE / VB / SAMPLES / PGUIDE / PROGWOB / PWOWIDG.CLS < prev   
Encoding:
Visual Basic class definition  |  1996-11-26  |  7.1 KB  |  218 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Widget"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = False
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11. ' >> Best viewed in Full Module view. <<
  12. '
  13. ' Storage for debug ID number.
  14. Private mlngDebugID As Long
  15. Implements IDebug
  16.  
  17. ' Defining error numbers in a public Enum
  18. '   makes them visible throughout the
  19. '   project.
  20. Public Enum WidgetErrors
  21.     wdgERRTaskCanceled = 1059
  22. End Enum
  23.  
  24. ' PercentDone event is raised periodically
  25. '   during LongTask, to notify the caller
  26. '   of progress.  The event arguments are
  27. '   the percent complete and a ByRef Cancel
  28. '   argument that the caller can set to
  29. '   True to cancel LongTask.
  30. Event PercentDone(ByVal Percent As Double, _
  31.     Cancel As Boolean)
  32.     
  33. ' LongTask method simulates a long-running
  34. ' --------      task that raises the
  35. '   PercentDone event, and allows the caller
  36. '   to cancel the operation.
  37. '
  38. ' The first argument tells LongTask how
  39. '   long you want the simulated task to
  40. '   last.  The second argument gives the
  41. '   minimum interval for raising events
  42. '   to notify the caller of progress.
  43. '
  44. ' Using a time interval to determine
  45. '   when to raise the event gives more
  46. '   consistent results on different
  47. '   computers.  For an alternate
  48. '   approach, see LongTask2.
  49. '
  50. Public Sub LongTask(ByVal Duration As Double, _
  51.         ByVal MinimumInterval As Double)
  52.     Dim dblThreshold As Double
  53.     Dim dblStart As Double
  54.     Dim blnCancel As Boolean
  55.     
  56.     dblStart = Timer
  57.     dblThreshold = MinimumInterval
  58.     
  59.     Do While Timer < (dblStart + Duration)
  60.         ' In a real application, a unit of
  61.         '   work would be done here.  The
  62.         '   work must be divided up so
  63.         '   that units are neither too large
  64.         '   (too long between notifications)
  65.         '   nor too small (the more times you
  66.         '   test, the less efficient LongTask
  67.         '   will be).
  68.         
  69.         ' After each unit of work, test to
  70.         '   see if it's time to notify the
  71.         '   caller of LongTask's progress.
  72.         If Timer > (dblStart + dblThreshold) Then
  73.             ' Raise the event; execution of
  74.             '   LongTask will not continue
  75.             '   until the caller's event
  76.             '   procedure returns!
  77.             RaiseEvent PercentDone( _
  78.                 dblThreshold / Duration, _
  79.                 blnCancel)
  80.             '
  81.             ' Test to see whether the caller
  82.             '   wants to cancel LongTask.
  83.             If blnCancel Then
  84.                 Err.Raise vbObjectError + wdgERRTaskCanceled, , _
  85.                     "Task Cancelled"
  86. ' NOTE: If your program breaks here, right-click
  87. '   to bring up the code window context menu.
  88. '   Click Toggle, then click Break on Unhandled
  89. '   Errors.  Press F5 to continue running the
  90. '   program.  (You may have to press Alt+Tab to
  91. '   get the Events form back.)  Here's why you
  92. '   toggle the setting:
  93. ' The default setting, Break in Class Module,
  94. '   is useful if you're getting an error on a
  95. '   call to a method of a class, because it allows
  96. '   Visual Basic to break INSIDE the class module,
  97. '   at the point of the error.  If your class
  98. '   raises errors routinely, as here, this is not
  99. '   so convenient!
  100. ' You can set the default to Break on Unhandled
  101. '   Errors, using the General tab of the Options
  102. '   dialog box, accessible from the Tools menu.
  103. '   If you do this, just remember that when you
  104. '   break on a method call, and you want to run
  105. '   to the point of the error, you can use
  106. '   the code window context menu to Toggle to
  107. '   Break in Class Module.
  108. ' Note that you can also use Alt+F5 to run past
  109. '   a single error when you're using Break in
  110. '   Class Module (or Alt+F8 to step past).  If
  111. '   these keys leave you at the same line of code,
  112. '   then there's no error handler available.
  113. ' For more information, see "Debugging Class
  114. '   Modules" in Books Online.
  115. ' [End Digression]
  116.             End If
  117.             '
  118.             ' Set the threshold for the next
  119.             '   notification.
  120.             dblThreshold = dblThreshold + MinimumInterval
  121.        End If
  122.     Loop
  123. End Sub
  124.  
  125. ' LongTask2 also simulates a long-running
  126. ' ---------     task that raises the
  127. '   PercentDone event, and allows the caller
  128. '   to cancel the operation.
  129. '
  130. ' The simulated task consists of repeated
  131. '   floating-point calculations.  The first
  132. '   argument tells LongTask2 how many
  133. '   iterations you want the task to have.
  134. '   The second argument gives the change
  135. '   in percentage complete that triggers
  136. '   the notification event.  Note that
  137. '   this method results in a variable
  138. '   length of time between notifications --
  139. '   a variation that may be compounded by
  140. '   differences in machine performance.
  141. '
  142. ' By contrast, LongTask uses a time
  143. '   interval to determine how often to
  144. '   raise the event; this gives more
  145. '   consistent results on different
  146. '   computers.
  147. '
  148. Public Sub LongTask2(ByVal Iterations As Long, _
  149.         ByVal PercentChange As Byte)
  150.     Dim lngThreshold As Long
  151.     Dim dblIterationsPerEvent As Double
  152.     Dim lngCt As Long
  153.     Dim dblDummy As Double
  154.     Dim blnCancel As Boolean
  155.     
  156.     dblIterationsPerEvent = Iterations _
  157.         * (CDbl(PercentChange) / 100)
  158.     lngThreshold = dblIterationsPerEvent
  159.     
  160.     For lngCt = 1 To Iterations
  161.         ' In a real application, a unit of
  162.         '   work would be done here.  The
  163.         '   work must be divided up so
  164.         '   that units are neither too large
  165.         '   (too long between notifications)
  166.         '   nor too small (the more times you
  167.         '   test, the less efficient LongTask
  168.         '   will be).
  169.         dblDummy = 3.14159 * 2640 * 2640
  170.         
  171.         ' After each unit of work, test to
  172.         '   see if it's time to notify the
  173.         '   caller of LongTask's progress.
  174.         If lngCt > lngThreshold Then
  175.             ' Raise the event; execution of
  176.             '   LongTask2 will not continue
  177.             '   until the caller's event
  178.             '   procedure returns!
  179.             RaiseEvent PercentDone( _
  180.                 lngCt * 100 / Iterations, _
  181.                 blnCancel)
  182.             '
  183.             ' Test to see whether the caller
  184.             '   wants to cancel LongTask2.
  185.             If blnCancel Then
  186.                 Err.Raise vbObjectError + wdgERRTaskCanceled, , _
  187.                     "Task Cancelled"
  188.             End If
  189.             '
  190.             ' Set the threshold for the next
  191.             '   notification.
  192.             lngThreshold = lngThreshold + dblIterationsPerEvent
  193.         End If
  194.     Next
  195. End Sub
  196.  
  197. Private Sub Class_Initialize()
  198.     mlngDebugID = DebugInit(Me)
  199. End Sub
  200.  
  201. Private Sub Class_Terminate()
  202.     DebugTerm Me
  203. End Sub
  204.  
  205. ' -------- IDebug Implementation --------
  206. '
  207. ' IDebug.DebugID gives you a way to tell
  208. ' ====== -------    objects apart.  It's
  209. '   required by the DebugInit, DebugTerm,
  210. '   and DebugShow debugging procedures
  211. '   declared in modFriend.
  212. '
  213. Private Property Get IDebug_DebugID() As Long
  214.     IDebug_DebugID = mlngDebugID
  215. End Property
  216.  
  217.  
  218.